package com.example.petstoreservice.service;

import com.example.petstoreservice.entity.Coupon;
import com.example.petstoreservice.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CouponService {

    @Autowired
    private CouponMapper couponMapper;

    public List<Coupon> getAllCoupons() {
        return couponMapper.getAllCoupons();
    }

    public List<Coupon> getCouponsByUsername(String username) {
        return couponMapper.getCouponsByUsername(username);
    }

    public boolean insertCoupon(Coupon coupon) {
        Date now = new Date();
        coupon.setCreateTime(now);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DAY_OF_YEAR, 7); // 加7天
        Date endTime = calendar.getTime();
        coupon.setEndTime(endTime);

        int affectedRows = couponMapper.insertCoupon(coupon);
        return affectedRows > 0;
    }

    public boolean updateCoupon(Coupon coupon) {
        int affectedRows = couponMapper.updateCoupon(coupon);
        return affectedRows > 0;
    }

    public boolean deleteCoupon(int couponId) {
        int affectedRows = couponMapper.deleteCoupon(couponId);
        return affectedRows > 0;
    }
}
